"""
LLM解析工具
负责解析LLM响应和生成默认解决方案
"""
import json
def parse_llm_solutions(llm_response):
    """解析LLM响应，提取JSON格式的解决方案"""
    try:
        # 尝试直接解析JSON
        if "{" in llm_response and "}" in llm_response:
            # 提取JSON部分
            start = llm_response.find("{")
            end = llm_response.rfind("}") + 1
            json_str = llm_response[start:end]
            parsed_data = json.loads(json_str)

            # 验证解决方案格式
            # 检查解析后的字典中是否存在键 "solutions"，且其值是列表类型
            if "solutions" in parsed_data and isinstance(parsed_data["solutions"], list):
                solutions = parsed_data["solutions"]

                # 验证每个解决方案的格式
                for solution in solutions:
                    # 判断是否为空
                    if not all(key in solution for key in ["id", "identify", "message_type", "content"]):
                        return None

                    content = solution["content"]
                    if not all(key in content for key in
                               ["root_cause_analysis", "nodes_to_call", "tools_required", "description"]):
                        return None

                return solutions

        return None
    # JSON 解码错误、键错误、类型错误
    except (json.JSONDecodeError, KeyError, TypeError) as e:
        print(f"解析LLM响应失败: {e}")
        return None

# 新增：解析boss_node大模型生成的llm_response中的JSON数据
def parse_boss_llm_response(llm_response):
    """解析boss_node大模型生成的llm_response中的JSON数据"""
    try:
        # 提取最外层JSON
        if "{" in llm_response and "}" in llm_response:
            start = llm_response.find("{")
            end = llm_response.rfind("}") + 1
            json_str = llm_response[start:end]
            parsed_data = json.loads(json_str)
            return parsed_data
        return None
    except (json.JSONDecodeError, KeyError, TypeError) as e:
        print(f"解析Boss LLM响应失败: {e}")
        return None

#"""生成默认的解决方案"""
# def generate_default_solutions(rag_knowledge):
#     """生成默认的解决方案"""
#     solutions = []
#
#     # 方案1：综合性能优化
#     solution1 = {
#         "id": 1,
#         "identify": ["cpu_node", "mem_disk_node", "processes_node"],
#         "message_type": "comprehensive_performance_optimization",
#         "content": {
#             "root_cause_analysis": rag_knowledge,
#             "nodes_to_call": ["cpu_node", "mem_disk_node", "processes_node"],
#             "tools_required": {
#                 "cpu_node": ["cpu_tool_1", "cpu_tool_2"],
#                 "mem_disk_node": ["mem_tool_1", "disk_tool_1"],
#                 "processes_node": ["process_tool_1", "process_tool_2"]
#             },
#             "description": "针对CPU、内存、磁盘和进程的综合性能优化方案"
#         }
#     }
#
#     # 方案2：网络与系统监控
#     solution2 = {
#         "id": 2,
#         "identify": ["net_node", "kernel_system_metrics_node", "log_node"],
#         "message_type": "network_system_monitoring",
#         "content": {
#             "root_cause_analysis": rag_knowledge,
#             "nodes_to_call": ["net_node", "kernel_system_metrics_node", "log_node"],
#             "tools_required": {
#                 "net_node": ["net_tool_1", "net_tool_2"],
#                 "kernel_system_metrics_node": ["kernel_tool_1", "system_tool_1"],
#                 "log_node": ["log_tool_1", "log_tool_2"]
#             },
#             "description": "针对网络、内核系统和日志的监控分析方案"
#         }
#     }
#     solutions.extend([solution1, solution2])
#     return solutions